package kafka

import (
	"strings"
	"time"

	kafkago "github.com/segmentio/kafka-go"
)

type KafkaConfig struct {
	Dsn string `yaml:"dsn" gorm:"comment:连接字符串"`
}

func GetKafkaWriter(topic string) *kafkago.Writer {
	return &kafkago.Writer{
		Addr:         kafkago.TCP(Config.Dsn),
		Topic:        topic,
		Balancer:     &kafkago.LeastBytes{},
		BatchSize:    10000,
		BatchTimeout: 10,
	}
}
func GetKafkaReader(topic, groupID string) *kafkago.Reader {
	brokers := strings.Split(Config.Dsn, ",")
	return kafkago.NewReader(kafkago.ReaderConfig{
		Brokers:  brokers,
		GroupID:  groupID,
		Topic:    topic,
		MinBytes: 10e3, // 10KB
		MaxBytes: 10e6, // 10MB
		MaxWait:  time.Millisecond * 100,
	})
}
